#include"Queue.h"

QueueNode* QueueNodeBuy(QueueDateType x)
{
	QueueNode* tem = (QueueNode*)malloc(sizeof(QueueNode));
	if (tem == NULL)
	{
		printf("malloc fail");
		exit(-1);
	}
	tem->date = x;
	tem->next = NULL;
	return tem;
}

void QueueInit(Queue* q)
{
	assert(q);
	q->head = NULL;
	q->tail = NULL;
}

void QueuePush(Queue* q, QueueDateType x)
{
	assert(q);
	QueueNode* tem = QueueNodeBuy(x);
	if (q->head == NULL)
	{
		q->head = tem;
		q->tail = tem;
	}
	else
	{
		q->tail->next = tem;
		q->tail = tem;
	}
}

void QueuePop(Queue* q)
{
	assert(q);
	assert(q->head);
	assert(q->tail);

	QueueNode* pop = q->head;
	q->head = q->head->next;
	free(pop);
	if (q->head == NULL)
	{
		q->tail = NULL;
	}
}

QueueDateType QueueFront(Queue* q)
{
	assert(q);
	assert(q->head);
	return q->head->date;
}

QueueDateType QueueBack(Queue* q)
{
	assert(q);
	assert(q->tail);
	return q->tail->date;
}

bool QueueEmpty(Queue* q)
{
	assert(q);
	return q->head == NULL;
}

void QueueDestroy(Queue* q)
{
	assert(q);
	while (q->head)
	{
		QueuePop(q);
	}
}

int QueueSize(Queue* q)
{
	assert(q);
	int size = 0;
	QueueNode* cur = q->head;
	while (cur)
	{
		cur = cur->next;
		size++;
	}
	return size;
}








